<%@ page contentType="text/html;charset=UTF-8" language="java" import="org.apache.shiro.SecurityUtils,com.cetc.nsfx.security.vo.UserContext"%>
<%
UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
%>
<html>
<head>
    <title>Title</title>
    <style type="text/css">
		.ztree li span.demoIcon{padding:0 2px 0 10px;}
		.ztree li span.button.icon01{margin:0; background: url(/media/image/btn/manager.png) no-repeat scroll 0 0 transparent; vertical-align:top; *vertical-align:middle}
	</style>
    <script type="text/javascript">
    
    var roleManager = function(){
    	var loginName='<%=user.getUserId() %>';
    	var userType='<%=user.getUser().get("USER_TYPE") %>';
        var treeFont = function(treeId, node) {
    		return node.font ? node.font : {};
    	}
        var initSwryTree = function(treeId){
        	var swryTreeSetting = {
        			async: {
        				enable: true,
        				type:'get',
//         				contentType: "application/json",
        				dataType: "json",
// 						contentType: "application/x-www-form-urlencoded",
        				url: ctx + "/sys/role/list.do",
        				autoParam: ["T_CODE"],
        				dataFilter: filter
        			},
        			check: {
        				enable: true,
        				chkStyle: "radio",
        				radioType: "level"
        			},
        			data: {
        				simpleData: {
        					enable : true,
        					idKey : "T_CODE",
        					pIdKey : "PARENT_DEPT_CODE"
        				}
        			},
        			view: {
        				selectedMulti: false
        			},
        			callback: {
        				beforeExpand: beforeExpand,
//         				onAsyncSuccess: onAsyncSuccess,
        				onClick: zTreeOnClick
        			}
        			
        		};
        	function filter(treeId, parentNode, msg) {
    			if (!msg) return null;
    			var childNodes = msg.aaData;
    			for (var i=0, l=childNodes.length; i<l; i++) {
    				childNodes[i].name = childNodes[i].T_NAME.replace(/\.n/g, '.');
    				if(childNodes[i].MENU_TYPE == '1'){
    					childNodes[i].isParent=true;
    					childNodes[i].nocheck=true;
    					childNodes[i].icon = ctx + "/media/image/btn/house.png";
					}else{
						childNodes[i].isParent=false;
						childNodes[i].icon = ctx + "/media/image/btn/user.png";
					}
    			}
    			return childNodes;
    		}
        	function beforeExpand(treeId, treeNode) {
    			if (!treeNode.isAjaxing) {
					var zTree = $.fn.zTree.getZTreeObj("rolelist");
    				zTree.reAsyncChildNodes(treeNode, "refresh");
    				return true;
    			} else {
    				alert("zTree 正在下载数据中，请稍后展开节点。。。");
    				return false;
    			}
    		}
    		/* function onAsyncSuccess(event, treeId, treeNode, msg) {
    			if (!msg || msg.length == 0) {
    				return '[]';
    			}
    			var zTree = $.fn.zTree.getZTreeObj("rolelist");
    			var nodes = msg.aaData;
    			
    		} */
        	$.ajax({
    			type : 'get',
    			dataType : 'json',
    			url : ctx + "/sys/role/list.do",
    			contentType : 'charset=utf-8',
    			success : function(msg) {
    				var data = msg.aaData;
    				$(data).each(function(i, n) {
    					if (n.MENU_TYPE==0) {
    						n.icon = ctx + "/media/image/btn/user.png";
    					} else {
    						n.icon = ctx + "/media/image/btn/house.png";
    					}
    					
    					n.name = n.T_NAME;
    					
    				});
    				var tree = $.fn.zTree.init($('#'+treeId), swryTreeSetting, data);
    				tree.expandAll(true);
					var level1nodes = tree.getNodes();
					for(var i=0;i<level1nodes.length;i++){
						if(level1nodes[i].MENU_TYPE == '1'){
							level1nodes[i].nocheck=true;
							if(!level1nodes[i].isParent){
								level1nodes[i].isParent=true;
							}
							tree.updateNode(level1nodes[i]);
						}
					}
					var nodes = level1nodes[0].children;
					if(undefined != nodes){
						for (var i=0, l=nodes.length; i < l; i++) {
							if(nodes[i].MENU_TYPE == '1'){
								nodes[i].isParent=true;
								nodes[i].nocheck=true;
								tree.updateNode(nodes[i]);
							}
						}
    				}
    			}
    		});
        	function zTreeOnClick(event, treeId, treeNode) {
        		if(treeNode.isParent){
        			return false;
        		}
        		var nodeCode = treeNode.T_CODE;
        		if(userType !='1' && userType != '2'){
        			$('#btn_user_set').html('');
        		}else if(nodeCode == loginName){
        			$('#btn_user_set').html('<i class="fa fa-search"></i>&nbsp;查看');
//         			$('#btn_user_set i').removeClass('fa-plus');
//         			$('#btn_user_set i').addClass('fa-search');
        		}else{
        			$('#btn_user_set').html('<i class="fa fa-plus"></i>&nbsp;设置');
        		}
        		var tree = $.fn.zTree.getZTreeObj(treeId);
        		treeNode.checked=true;
        		tree.updateNode(treeNode);
        		roleManager.initStationTree('stationlist',nodeCode);
        		roleManager.initMenuTree('menulist',nodeCode);
        	};
    	}
        
        var initStationTree = function(treeId,nodeid){
        	var stationTreeSetting = {
        			check: {
        				enable: false
        			},
        			data: {
        				simpleData : {
        					enable : true,
        					idKey : "ROLE_CODE",
        					pIdKey : "PARENT_CODE",
        					rootPId : '0'
        				}
        			},
        			view: {
        				addDiyDom: addDiyDom
        			},
        			callback: {
//         				onClick: stationOnClick
        			}
        		};
        	$.ajax({
    			type : 'get',
    			dataType : 'json',
    			url : ctx + "/sys/role/userStationData.do?principalName="+nodeid,
    			contentType : 'charset=utf-8',
    			success : function(msg) {
    				var data = msg.aaData;
    				$(data).each(function(i, n) {
    					if (n.ROLE_TYPE==0) {
    						n.icon = ctx + "/media/image/btn/folder.gif";
    					} else {
    						n.icon = ctx + "/media/image/btn/leaf.gif";
    					}
    					
    					n.name = n.ROLE_NAME;
    					
    				});
    				var tree = $.fn.zTree.init($('#'+treeId), stationTreeSetting, data);
    				tree.expandAll(true);
    			}
    		});
        
        	function addDiyDom(treeId, treeNode) {
    			var aObj = $("#" + treeNode.tId + "_a");
    			if (!treeNode.isParent && treeNode.IS_DELEGATE=='1') {
    				var editStr = "<span class='demoIcon' title='权限委派' ><span class='button icon01'></span></span>";
    				$(aObj).append(editStr);
    			} 
    		}
    	}
        
        var initMenuTree = function(treeId,nodeid){
        	var menuTreeSetting = {
        			check: {
        				enable: false
        			},
        			data: {
        				simpleData : {
        					enable : true,
        					idKey : "MENU_CODE",
        					pIdKey : "PARENT_CODE",
        					rootPId : '0'
        				}
        			}
        		};
        	$.ajax({
    			type : 'get',
    			dataType : 'json',
    			url : ctx + "/sys/role/roleMenuData.do?principalName="+nodeid,
    			contentType : 'charset=utf-8',
    			success : function(msg) {
    				var data = msg.aaData;
    				$(data).each(function(i, n) {
    					if (n.MENU_TYPE == '1') {
    						n.icon = ctx + "/media/image/btn/folder.gif";
    					} else {
    						n.icon = ctx + "/media/image/btn/menu.png";
    					} 
    					
    					n.name = n.MENU_NAME;
    					
    				});
    				var tree = $.fn.zTree.init($('#'+treeId), menuTreeSetting, data);
    				tree.expandAll(true);
    			}
    		});
    	}
        	return {
        		initSwryTree:function(treeId){
        			return initSwryTree(treeId);
        		},
        		initStationTree:function(treeId,nodeid){
	    			return initStationTree(treeId,nodeid);
	    		},
	    		initMenuTree:function(treeId,nodeid){
	    			return initMenuTree(treeId,nodeid);
	    		}
        	} 
        }();
	    
		$(function(){
	    	
			roleManager.initSwryTree('rolelist');
	    	$('.chosen-select').chosen();
	    
	    });
		
    </script>
    <script type="text/javascript">
function stationSet(){	
	var zTree = $.fn.zTree.getZTreeObj("rolelist");
	var nodes = zTree.getSelectedNodes();
	if(undefined == nodes){
		alert("请选择用户！");
		return false;
	}
	if(nodes[0].isParent || nodes.length <1){
		alert("请选择用户！");
		return false;
	}
	$('#user_loginName').val(nodes[0].T_CODE);
	var isdisabled =false;//与当前登录用户一致禁用复选框
	var loginName='<%=user.getUserId() %>';
	if(nodes[0].T_CODE == loginName){
		isdisabled = true;
		$('.save').hide();
	}else{
		$('.save').show();
	}
	userStationSeting(nodes[0].T_CODE,isdisabled);
	show_sys_modal('sysUserStationModal');
}
function userStationSeting(loginName,isdisabled){
	var BSAEURL = $("#BaseURL").val();
	$('#sysuserstationset').dataTable({
		"bDestroy": true,
		"bSortClasses" : false,
		"bSort" : false,
		"bProcessing" : false,
		"bServerSide" : false,
		"bAutoWidth" : true,
		"bFilter" : false,
		"bInfo" : false,
		"bLengthChange" : false,
		"bPaginate" : false,
		"sServerMethod" : "POST",
		"sAjaxSource" : ctx + "/sys/role/rolelist.do",
		"fnServerParams": function ( aoData ) {
	    	   	aoData.push({ "name": "loginName", "value": loginName });
	    	   	aoData.push({ "name": "number", "value": Math.random() });
		    },
		"fnDrawCallback":function(){
			 if(isdisabled){
				$('#sysuserstationset').find('input[type="checkbox"]').each(function(){
					$(this).attr("disabled","disabled");
				});
			}  
		},
		"aoColumns" : [ {
			"mData" : 'NAME_A',
			"sWidth" : "70%",
			"mRender":function(data, type, row){
				var isCheck = row.PRINCIPAL_NAME_A;
				if(isCheck != null && isCheck != ''){
					return '<label ><input type="checkbox" name="m_checkObj" value="'+row.CODE_A+'" checked onclick="setChecked(this)">'+data+'</label>';
				}else{
					return '<label ><input type="checkbox" name="m_checkObj" value="'+row.CODE_A+'"  onclick="setChecked(this)">'+data+'</label>';
				}
		    }
		}, {
			"mData" : 'IS_DELEGATE_A',
			"sWidth" : "30%",
// 			"bVisible":isdelegate,
			"mRender":function(data, type, row){
				if(Number(data) == 1){
					return '<label><input type="checkbox" name="m_delegate" id="'+row.CODE_A+'" checked></label>';
				}else{
					var isCheck = row.IS_DELEGATE_A;
					if(isCheck != null && isCheck != ''){
						return '<label><input type="checkbox" name="m_delegate" id="'+row.CODE_A+'"></label>';
					}else{
						return '<label><input type="checkbox" name="m_delegate" id="'+row.CODE_A+'" disabled></label>';
					}
				}
		    }
		} ]
	});
};

function getCheckedValRole(){
		
	    //获取选中值
	    var idValue=''; 
	    $('#sysuserstationset input[type="checkbox"]:checked').each(function(){
	  	  var chkName = this.name;
	  	  if(chkName != 'm_delegate'){
	  		  var chkVal = this.value;
// 	  		alert('2='+$('#'+chkVal).is(':checked'));
// 	  		alert($('#'+chkVal)[0].checked);
	  		  var isdeleg='false';
	  		  if($('#'+chkVal).is(':checked')){
	  			  isdeleg ='true';
	  		  }
	      	  idValue += chkVal+","+isdeleg+"#";
	  	  }
	    }); 
	    return idValue;
	}

function setChecked(e){
	var chkName = e.name;
	  var chkVal = e.value;
	  if(e.checked){
		  $("#"+chkVal).removeAttr("disabled");
	  }else{
// 		  $("#"+chkVal).checked=false;
// 		  $('#'+chkVal)[0].checked=false;
		  $("#"+chkVal).attr("checked",false);
		  $("#"+chkVal).attr("disabled","disabled");
	  } 
};
	//保存按钮点击事件处理
	function saveStation(){
		var zTree = $.fn.zTree.getZTreeObj("rolelist");
		var nodes = zTree.getSelectedNodes();
	    var roleIds = getCheckedValRole();
	    $.ajax({
			type : "post",
			url : ctx + "/sys/role/save.do",
			data: {'stationID':roleIds,'userLoginName':nodes[0].T_CODE},
			dataType : "json",
			success : function(data) {
				if (data.success == "success") {
					notyMsg("保存用户权限数据成功!","danger");
					$("#sysUserStationModal").modal('hide');
					roleManager.initStationTree('stationlist',nodes[0].T_CODE);
	        		roleManager.initMenuTree('menulist',nodes[0].T_CODE);
				}else{
					notyMsg("保存部门/用户权限数据失败!","error");
				}
			},
			error : function( objAJAXRequest, strError) {
				notyMsg("保存部门/用户权限数据失败!","error");
			}
		});
    
	};
</script>
</head>
<body>
<div class="content-wrapper">
    <h3>系统管理/权限管理</h3>
    <div class="row">
    	<div class="col-lg-4">
	    	<div class="panel panel-info">
	    	<div class="panel-heading">
	    		<div class="text-left">
			          <em class="fa fa-users fa-1x">&nbsp;组织/人员</em>
			       </div>
	    	</div>
	    		<div class="panel-body">
			       <ul id="rolelist" class="ztree" style="height: 450px;overflow-y: auto"></ul>
			    </div>
	    	</div>
    	</div>
    	<div class="col-lg-4">
	    	<div class="panel panel-info">
	    	<div class="panel-heading">
	    		<div class="text-left">
			          <em class="fa fa-user fa-1x">&nbsp;岗位菜单</em>
			       </div>
	    	</div>
	    		<div class="panel-body">
			       <ul id="stationlist" class="ztree" style="height: 405px;overflow-y: auto"></ul>
			       <p class="pull-right">
						<a id="btn_user_set" type="button" onclick="stationSet()" class="btn btn-info" ><i class="fa fa-plus"></i>&nbsp;设置</a>
<!--               			<a id="btnSave" onclick="editDeptEmp()" type="button" class="btn btn-success"><i class="fa fa-check"></i>&nbsp;保存</a> -->
				   </p>
			    </div>
	    	</div>
    	</div>
    	<div class="col-lg-4">
	    	<div class="panel panel-info">
	    	<div class="panel-heading">
	    		<div class="text-left">
			          <em class="fa fa-cogs fa-1x">&nbsp;功能权限菜单</em>
			       </div>
	    	</div>
	    		<div class="panel-body">
					<ul id="menulist" class="ztree" style="height: 450px;overflow-y: auto"></ul>			       
			    </div>
	    	</div>
    	</div>
    </div>
   
</div>

<!-- 岗位设置 -->

	
	<div class="modal fade" id="sysUserStationModal" data-backdrop="static">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">用户岗位设置</h4>
      </div>
      <div class="modal-body" style="height: 400px;overflow-y:auto; ">
      <div>
      		<table class="table table-bordered display"  id="sysuserstationset">
				<thead>
					<tr>
						<th>岗位名称</th>
						<th>是否委派</th>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
      </div>
       	
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button onclick="saveStation()" type="button" class="btn btn-primary save">保存</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div>
</body>

</html>